<?php
/**
 * Base_flexmodel Model
 * Define base function of database interactivity
 *
 * @package		Model
 * @author		F. Harsanto
 * @copyright	Copyright (c) 2010, StreetDirectory.com
 * @link		http://www.streetdirectory.com
 * @since		Version 1.0
 * @filesource
 */
	
// Modified by Jev 21Mar12
	
/**
* @property DB_active_record $db
* */
class Base_flexmodel extends CI_Model 
{
	protected $parent_table;
	protected $table_name;
	var $order_fields;
	var $order_dirs;
	var $id_field;
	var $title_field;
	
    function __construct()
    {
        parent::__construct();
		$this->order_fields = array();
		$this->order_dirs = array();
		
		$this->id_field = null;
		$this->title_fiels = null;
	}
	
	function init($table_name,$id_field,$title_field)
	{
		$this->id_field = $id_field;
		$this->table_name = $table_name;
		$this->title_field = $title_field;
	}
	
	/**
	* mostly get name or get description by id
	*/
	function get_title($id)
	{
		if($this->title_field!=null && $this->id_field!=null)
		{
			$rec = $this->db->select($this->title_field)->from($this->table_name)->where($this->id_field,$id)->get();
			if($rec->num_rows()>0)
			{
				$row = (array) $rec->row();
				return $row[$this->title_field];
			}
			return FALSE;
		}
		return FALSE;
	}
	
	function set_table_name($name) 
	{
		$this->table_name = $name;
		return $this;
	}

    function get_table_name()
    {
        return $this->table_name;
    }
	
	function build_query($params, $count=false, $db=null) 
	{
		if(!isset($db) || !$db)
			$db = $this->db;
		if (isset($params->swhere) && $params->swhere != null)
			$db->where($params->swhere);
		if ($count) return;
		if (isset($params->sortname) && trim($params->sortname) != NULL && isset($params->sortorder))
			$db->order_by($params->sortname, $params->sortorder);
		if (isset($params->rp) && isset($params->limitstart))
			$db->limit($params->rp, $params->limitstart);
//		if ($count) 
//			$this->db->count_all_results();			
	}
	
	function is_affected() 
	{
		return ($this->db->affected_rows() > 0 ? true : false);
	}
	
//	function get($order_by=null,$order_dir="asc") 
//	{
//		if ($this->table_name != null) 
//		{
//			$this->db->select("*")->from($this->table_name);
//			if($order_by!=null)
//			{
//				$this->db->order_by($order_by,$order_dir);
//			}
//			return $this->db->get();
//		}
//		return FALSE;
//	}


	/**
	 * Modified by Jev, 27Mar12
	 * Modifying for multiple order by
	 * @param type $order_by
	 * @param type $order_dir
	 * @return type 
	 */
	function get($order_by=null,$order_dir="asc") 
	{
		if ($this->table_name != null) 
		{
			$this->db->select("*")->from($this->table_name);
			if($order_by!=null)
			{
				$this->set_order($order_by,$order_dir);
			}
			if(count($this->order_dirs)>0)
			{
				for($i=0;$i<count($this->order_dirs);$i++)
				{
					$this->db->order_by($this->order_fields[$i],$this->order_dirs[$i]);		
				}
			}
			return $this->db->get();
		}
		return FALSE;
	}
	
    function get_search($fields, $where=NULL, $show_field=NULL)
    {
        if (is_array($fields) && count($fields) > 0)
        {
            foreach ($fields as $key => $match)
            {
                $this->db->like($key, $match);
            }
            return $this->_get_where($where, $show_field);
        }
        return FALSE;
    }
	
    function get_where($where=NULL, $fields=NULL, $offset=NULL, $limit=NULL)
    {
        return $this->_get_where($where, $fields, $offset, $limit);
    }

	function _get_where($where=NULL, $fields=NULL, $offset=NULL, $limit=NULL)
	{
		if ($this->table_name != null) 
		{
			$this->db->select($fields != NULL ? $fields : "*")
				->from($this->table_name);
			if ($where != NULL)
			{
				$this->db->where($where);
			}
			if ($offset != NULL && $limit > 0)
			{
				$this->db->limit($limit, $offset);
			}
			
			if(count($this->order_dirs)>0)
			{
				for($i=0;$i<count($this->order_dirs);$i++)
				{
					$this->db->order_by($this->order_fields[$i],$this->order_dirs[$i]);		
				}
			}
		
			$q = $this->db->get();
			if ($q->num_rows() > 0) 
			{
				return $q->result();
			}
		}
		return FALSE;
	}
	
	function get_where_in($where=NULL, $fields=NULL, $offset=NULL, $limit=NULL)
    {
        return $this->_get_where_in($where, $fields, $offset, $limit);
    }

	/**
	 * Added by Jev, 26Mar12
	 * @param type $where
	 * @param type $fields
	 * @param type $offset
	 * @param type $limit
	 * @return type 
	 */
	function _get_where_in($where=NULL, $values=NULL, $fields=NULL, $offset=NULL, $limit=NULL)
	{
		if ($this->table_name != null) 
		{
			$this->db->select($fields != NULL ? $fields : "*")
				->from($this->table_name);
			if ($where != NULL && $values != NULL)
			{
				$this->db->where_in($where , $values);
			}
			if ($offset != NULL && $limit > 0)
			{
				$this->db->limit($limit, $offset);
			}
			
			if(count($this->order_dirs)>0)
			{
				for($i=0;$i<count($this->order_dirs);$i++)
				{
					$this->db->order_by($this->order_fields[$i],$this->order_dirs[$i]);		
				}
			}
		
			$q = $this->db->get();
			if ($q->num_rows() > 0) 
			{
				return $q->result();
			}
		}
		return FALSE;
	}

	
	
	
	
	
	
	
	function get_detail($where=NULL, $fields=NULL)
	{        
		if ($this->table_name != null)
		{
			$this->db->select($fields != NULL ? $fields : "*")
				->from($this->table_name);
			if ($where != NULL)
			{
				$this->db->where($where);
			}
            //echo $this->db->to_string() ."<br>";
			$q = $this->db->get();
			if ($q->num_rows() > 0)
			{
				return $q->row();
			}
		}
		return FALSE;
	}

    function get_item($where, $field='name')
    {
        $row = $this->get_detail($where, $field);
        if ($row && isset($row->$field))
        {
            return $row->$field;
        }
        return '';
    }

    function get_string($field, $where=NULL, $separator="|")
    {
        $rows = $this->_get_where($where, $field);
        if ($rows)
        {
            $list = array();
            foreach ($rows as $row)
            {
                array_push($list, $row->$field);
            }
            return implode($separator, $list);
        }
        return '';
    }
	
	function update($data, $where) 
	{
		if ($this->table_name != null)
			$this->db->update($this->table_name, $data, $where);
	}
	
	function insert($data) 
	{
		if ($this->table_name != null) 
		{
			$this->db->set($data);
			$this->db->insert($this->table_name);
			return $this->db->insert_id();
		} 
	}
	
	function set_order($order_field,$order_dir="asc")
	{
		$this->order_fields[] = $order_field;
		$this->order_dirs[] = $order_dir;
	}
	
	function delete($where) 
	{
		if ($this->table_name != null)
			$this->db->delete($this->table_name, $where);
	}
	
	function set_join($join_table_name , $cond , $type = '')
	{
		$this->db->join( $join_table_name , $cond , $type);
	}
	
	
	
}
?>